home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / obsolete / stepwise.pro < prev    next >
Text File  |  1997-07-08  |  12KB  |  479 lines

  1. ; $Id: stepwise.pro,v 1.2 1997/01/15 04:02:19 ali Exp $
  2. ;
  3. ;  Copyright (c) 1991-1997, Research Systems Inc.  All rights
  4. ;  reserved. Unauthorized reproduction prohibited.
  5.  
  6.  
  7.   pro writereport,X,Y,V,Enter,direction,RepType,unit
  8. ;Writereportprints out 
  9.  
  10.  printf,unit," "
  11.  printf,unit," "
  12.  printf,unit," "
  13.  printf,unit,RepType," Report"
  14.  printf,unit,direction ,Enter
  15.  printf,unit,""
  16.  regression,X,Y,VarNames=V,Unit=unit
  17.  return
  18.  end
  19.  
  20.  
  21. function setnames,vn,N,unit
  22. ; if vn is an array with n names , simply return vn,else
  23. ; pad out vn with var i.
  24.  
  25.  NC=Size(vn)
  26.  
  27.  if(NC(1) NE 0 ) THEN BEGIN        ; 
  28.    VarName=vn
  29.    if(NC(1) LT N) THEN BEGIN
  30.      printf,unit,'stepwise-missing Variable names'
  31.      I=Indgen(N)
  32.      VarName=[VarName,'Var'+STRTRIM(I(NC(1):N-1),2)]
  33.    ENDIF
  34.  ENDIF ELSE VarName='Var'+ STRTrim(INDgen(N),2)
  35.  
  36.  return,VarName
  37.  END
  38.  
  39.  
  40. function mult_cor,A,index,k
  41. return, A(k,k) - A(index,k)*A(k,index)/A(index,index)
  42. end
  43.  
  44.  
  45.  
  46. pro InsertVar,A,index,k
  47.  
  48.  mult0 = 1.0/A(index,index)
  49.  m1 = A(*,index) # replicate(1.0,k+1)
  50.  m2 = replicate(1.0,k+1)#A(index,*)
  51.  mult1 = m1*m2
  52.  case index of
  53.  0: A(1:*,1:*) = A(1:*,1:*) - mult0*mult1(1:*,1:*) 
  54.  k: A(0:k-1,0:k-1) = A(0:k-1,0:k-1) - mult0*mult1(0:k-1,0:k-1) 
  55.  ELSE:BEGIN
  56.        IN = [lindgen(index),lindgen(k-index) + index+1 ]
  57.        A(IN,0:INDEX-1) =  A(IN,0:INDEX-1) -   $
  58.                           mult0*mult1(IN,0:Index-1)
  59.        A(IN,INDEX+1:*) =  A(IN,INDEX+1:*) -  $
  60.                           mult0*mult1(IN,INDEX+1:*)
  61.       END
  62.  ENDCASE
  63.  A(index,*) = -A(index,*) * mult0
  64.  A(*,index) = A(*,index) * mult0
  65.  A(index,index) = mult0
  66. return
  67. end
  68.  
  69.  
  70.  
  71. pro UpDate, COR, INEQ, N,K
  72. ; update correlation matrix by adding variables in  $
  73. ; InEQ to equation
  74.  
  75. for i = 0L,n-1 do insertvar,COR,INEQ(i),k
  76. return
  77. end
  78.  
  79.  
  80. function remove_from, A, B,VarNo
  81. ; View both A and B as sets and return the set compliment B-A
  82. ; elements not returned in same order
  83.  
  84. Temp = fltarr(VarNo + 1)
  85. Temp(B) = 1
  86. Temp(A) = 0
  87. return, where(Temp eq 1)
  88. END  
  89.  
  90.  
  91. function remove1_from, A, B
  92. ; View both A and B as sets and return the set compliment B-A
  93. ; elements not returned in same order
  94.  
  95.  A = A(sort(A))
  96.  B = B(sort(B))
  97.  
  98.  SA = N_Elements(A)
  99.  SB = N_Elements(B)
  100.  
  101.  if SB EQ 0 THEN return,B                   $
  102.   else if SB EQ 1 THEN if A(0) EQ B(0) THEN return,C                $
  103.                        else return,B
  104.  
  105. i = 0
  106. j = 0
  107.  
  108. while i LT SA and j LT SB do BEGIN
  109.  
  110.      if A(i) EQ B(j) THEN BEGIN              ; remove B(j) from B
  111.         if j EQ 0 THEN B = B(1:*)            $
  112.          else if j EQ SB-1 THEN B = B(0:SB-2)         $
  113.               else B = [B(0:j-1), B(j+1:*)]  
  114.         i = i + 1
  115.         SB = SB - 1
  116.      ENDIF ELSE if A(i) GT B(j) THEN j = j+1  $
  117.                 else i = i+1
  118. ENDWHILE
  119.  
  120. return,B
  121. END
  122.   
  123.  
  124.  
  125.  
  126.  
  127.  
  128. function f_to_exit,C,index,COR,k,n
  129. ; f_to_exit returns the index of the variable in C with
  130. ; smallest F_to_exit value which is computed via the
  131. ; partial correlation coefficient of each variable in
  132. ;  C against Y and adjusted for the remaining
  133. ; variables in C. If C is empty, inf is returned
  134.  
  135.  SC = size(C)
  136.  SC = SC(1)
  137.  
  138.  FMin = 1.0e30
  139.  index=0
  140.  mul = n - SC - 1
  141.  
  142.  P2 = Cor(k,k)
  143.  if N_Elements(C) EQ 0 THEN return,1.0e30
  144.  
  145.  for i = 0L,SC-1 DO BEGIN
  146.     P1 = mult_cor(COR,C(i),k) 
  147.     F = (P1-P2)/P1
  148.     F = (F * mul) / (1 - F)    
  149.     if (F LT FMin) THEN BEGIN
  150.         FMin = F
  151.         index = i
  152.     ENDIF
  153. ENDFOR
  154.  
  155. return, FMIN
  156. END
  157.           
  158.  
  159.  
  160.  
  161.  function f_to_enter, X,index,COR,k,n,SC
  162. ; f_to _enter returns the maximum f_to_enter value for the
  163. ; Variables in x ,index returns the index of the var with
  164. ; max f_to_enter
  165.  
  166.  SX1 = size(X)
  167.  SX = SX1(1)
  168.  FMax = 0
  169.  mul = n-2-SC
  170.  P1 = COR(k,k)
  171.  for i = 0L, SX-1 DO BEGIN
  172.      P2 = mult_cor(Cor,X(i),k)
  173.      F = (P1-P2)/P1
  174.      if F ge 1 THEN BEGIN
  175.        index = i
  176.        return,1.0e30
  177.      ENDIF
  178.  
  179.        F = (F * mul) / (1 - F)
  180.        if (F GT FMax) THEN BEGIN
  181.           FMax = F
  182.           Index = i
  183.        ENDIF
  184.       ENDFOR
  185.  
  186. return,FMax
  187. END
  188.  
  189.   
  190.  
  191.       
  192.  pro stepwise,X1,Y1,InEQ,alpha,alphar,VarNames=VN, $
  193.                   Report=RP, List_Name =LN,Missing=M
  194. ;+
  195. ; NAME:
  196. ;    STEPWISE
  197. ;
  198. ; PURPOSE:
  199. ;    To select the locally best set of independent variables to be used
  200. ;    in a regression analysis to predict Y.
  201. ;
  202. ; CATEGORY:
  203. ;    Statistics.
  204. ;
  205. ; CALLING SEQUENCE:
  206. ;    STEPWISE, X, Y [,InEQ, Alpha, Alphar]
  207. ;
  208. ; INPUT:
  209. ;    X:    A 2-dimensional array of observed variable values. 
  210. ;        X(i,j) = the value of the ith variable in the jth observation.
  211. ;
  212. ;    Y:    A column vector of observed dependent variable values. 
  213. ;        X and Y must have the same number of rows.
  214. ;
  215. ; OPTIONAL INPUTS:
  216. ;    InEq:    Vector of indices of equations to be forced into the equation.
  217. ;
  218. ;    Alpha:    Significance level for including a variable.  The default 
  219. ;        is 0.05.
  220. ;
  221. ;    Alphar:    Significance level for removing a variable from the 
  222. ;        regression equation. The default is equal to Alpha.
  223. ;
  224. ; KEYWORDS:   
  225. ;     VARNAMES:    A vector of names for the independent variables to be used in
  226. ;        the output.
  227. ;         
  228. ;    REPORT:    A flag to print out regression statistics as each a variable 
  229. ;        enters or leaves the equation.
  230. ;
  231. ;    LIST_NAME:    A string that contains the name of output file.  Default is 
  232. ;        to the screen.
  233. ;
  234. ;      MISSING:    Value of missing data.  If this keyword is not set, assume no
  235. ;        missing data.  Listwise handling of missing data.    
  236. ;
  237. ; OUTPUT:
  238. ;    Regression statistics for the final equation.
  239. ;
  240. ; OPTIONAL OUTPUT PARAMETERS:
  241. ;    InEq:    Vector of indices of variables in final equation.
  242. ;
  243. ; RESTRICTIONS:
  244. ;    None.
  245. ;
  246. ; COMMON BLOCKS:
  247. ;    None.
  248. ;
  249. ; SIDE EFFECTS:
  250. ;    None. 
  251. ; PROCEDURE:                     
  252. ;    Adapted from an algorithm from Statistical Analysis, a Computer
  253. ;    Oriented Approach, by A.A. Afifi and S.P. Azen.  The procedure 
  254. ;    successively picks the best predictor of the dependent variable Y 
  255. ;    given the variables already entered (via a statistic based on the
  256. ;    partial correlation coefficient).  Variables may be removed from the
  257. ;    equation if their significance falls below a given level. 
  258. ;-         
  259.  
  260.  On_Error,2    
  261.  
  262.  
  263.  if ( N_Elements(LN) NE 0 ) THEN openw,unit,/get,LN    $
  264.  else unit = -1 
  265.  
  266.  if N_ELEMENTS(X1) eq 0 THEN BEGIN
  267.    printf,unit,"stepwise-- Data array is empty"
  268.    goto,done0
  269.  ENDIF
  270.  
  271.  X = X1
  272.  SX = size(X)
  273.  S1 = SX(1)
  274.  S2 = SX(2)
  275.  SY = size (Y1)
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  if (SY(0) LT 2) THEN         BEGIN
  282.   Y = transpose(Y1)
  283.   SY = size(Y)
  284.  ENDIF ELSE Y = Y1
  285.    
  286.  
  287.  if  SY(0) NE 2 OR SX(0) NE 2 OR S2 NE SY(2) THEN   BEGIN
  288.     printf,unit," stepwise - incompatible arrays"
  289.     goto, DONE0
  290.  ENDIF
  291.  
  292.  if N_Elements(M) THEN BEGIN
  293.    X = listwise([x,y],M,rownum,rows,here)  ; Remove cases with 
  294.                                            ; missing data.
  295.                                            ; Remove and save
  296.                                            ; corresponding
  297.                                            ; values from y
  298.    X = X(0:S1-1,*)
  299.    if N_elements(X) le 1 THEN BEGIN 
  300.       printf,unit,'stepwise---too many missing data values,'
  301.       printf,unit,'           need more observations'
  302.       Goto, DONE0
  303.    ENDIF
  304.                 
  305.    yval = Y(rownum) 
  306.    Y = Y(0,here)                          
  307.    SX = size(X)
  308.    S2 = SX(2)
  309.    SY = size (Y)
  310.  ENDIF
  311.  
  312.  COR = Correl_Matrix([X,Y])
  313.  D = Determ(COR)
  314.  
  315.  if ( abs(D) lt 1.0e-7) THEN BEGIN
  316.     printf,unit, "Determinat of correlation matrix is", D
  317.     printf,unit, " Hit control C to terminate computations" 
  318.  endif
  319.  
  320.  
  321.  
  322.  outnum = S1-1
  323.  
  324.  
  325.  
  326.  if ( N_Elements(RP) NE 0) THEN RP =1 else RP =0
  327.  
  328.  Vars = setnames(VN,S1,unit)               ;Set up variable names
  329.                                           ; for output.
  330.  
  331.  if ( N_Elements(alpha) EQ 0) THEN alpha =.05
  332.  if ( N_Elements(alphar) EQ 0) THEN alphar = alpha   
  333.  
  334.  NotinEQ = INDGEN( S1)                    ; Initially, no   
  335.                                           ; variables in    
  336.                                           ; regression eq.
  337.  
  338.  
  339.  NF = N_Elements(InEq)                  ; Get number of
  340.                                         ; variables to 
  341.                                         ; force into equation
  342.  innum = NF + 1
  343.  if ( NF EQ S1) THEN goto,done       ; If all vars are forced,
  344.                                      ; exit
  345.  
  346.  if NF NE 0  THEN BEGIN               ; Are there variables to
  347.                                       ; force  into eq$
  348.  
  349.  
  350. NotInEq = remove_from( InEq, NotInEq,S1)  ;If so,remove them 
  351.                                           ;from NotIneq list.
  352.    outnum = outnum - NF
  353. Update,COR,INEQ,NF,S1             ; Update correlation matrix
  354.  ENDIF
  355.  
  356.  
  357.  if NF NE 0 THEN C = X(InEq,*)
  358.  
  359. if (innum gt 1) THEN      $
  360.  F = f_to_enter(NotInEQ,index,COR,S1,S2,innum-1)  $
  361.                                   ;Index specifies first .
  362. ELSE BEGIN
  363.   F = max(abs(COR(S1,0:S1-1)),index)
  364.   if F ne 1 THEN F = F^2*(S2-innum-1)/(1-F^2) else F = 1.0e30
  365. END
  366.  
  367.  F = 1 - f_test1(F,1,S2-2)              
  368.                                   ;Compute significance of F.
  369.  
  370.                                          
  371.  
  372.  if ( F LT alpha) THEN BEGIN
  373.    Update,COR,NotInEq(index),1,S1
  374.    if (NF  NE 0) THEN BEGIN  ; if significant,
  375.        InEq =  [InEq,NotInEq(index)]                 
  376.    ENDIF else           $
  377.          InEq = [NotInEq(index)]
  378.  ENDIF else if (NF NE 0) THEN goto, DONE  $
  379.             else  BEGIN
  380.                  printf,unit,                                $
  381.  "stepwise- no variable significant, mean best predictor of y"
  382.       goto, DONE0;
  383.            ENDELSE 
  384.  
  385.  
  386.  if RP NE 0 THEN writereport,X(InEq,*),Y,Vars(InEq)    $
  387.             ,Vars(NotInEq(index)),     $
  388.                 "Entering Variable: ","Step", unit  
  389.  
  390.  if (outnum EQ 0) THEN goto,DONE           ;All vars in eq?
  391.  
  392.  
  393.  if ( Index EQ 0) THEN NotInEq = NotInEQ(1:*)     $
  394.                                           ; remove from list 
  395.    else if (Index EQ outnum) THEN NotInEq =     $
  396.                                NotInEq(0:outnum-1) $
  397.      ELSE NotInEq = [NotInEq(0:index-1),NotInEq(index+1:*)]
  398.  
  399.  
  400.  
  401.  
  402.  while ( innum NE S2-2 ) DO BEGIN 
  403.       F = f_to_enter(NotInEq,index,COR,S1,S2,     $
  404.                      N_Elements(InEq))      
  405.                                     ; Get next var to enter
  406.       F = 1 - f_test1(F,1,S2-2-innum)
  407.  
  408.      if ( F LT alpha) THEN BEGIN
  409.        INEQ = [Ineq,NotInEq(index)]   ; put in eq
  410.        Update,COR,NotInEQ(index),1,S1
  411.       ENDIF else goto, DONE  
  412.  
  413.      if RP NE 0 THEN writereport,X(InEq,*),Y,Vars(InEq)   $
  414.        ,Vars(NotInEq(index)),"Entering Variable: ","Step",unit
  415.  
  416.  
  417.      innum = innum +1
  418.      outnum = outnum -1 
  419.  
  420.                           
  421.      if outnum EQ 0 THEN goto,done
  422.  
  423.      if ( Index EQ 0) THEN NotInEq = NotInEQ(1:*)     $
  424.                      ; Remove it from list.
  425.  
  426.      else if (Index EQ outnum) THEN NotInEq =  $
  427.                               NotInEq(0:outnum-1) $
  428.               ELSE NotInEq = [NotInEq(0:index-1),  $
  429.                               NotInEq(index+1:*)]
  430.       
  431.      F = f_to_exit(InEq(NF:*),index,COR,S1,S2)        
  432.                         ;Are other vars still significant?
  433.  
  434.      WHILE ( 1-f_test1(F,1,S2-innum-1) GT alphar) DO BEGIN
  435.  
  436.         if NF NE 0 THEN temp = InEq(0:NF-1)  
  437.                        ; if not, remove
  438.                                         ;them from eq
  439.         innum = innum-1
  440.         outnum = outnum +1
  441.         Varout = InEq(NF + Index)
  442.         NotInEq = [NotInEQ,VarOut]
  443.         Update,COR,varout,1,S1   
  444.         if (Index EQ 0) THEN InEq = InEq(NF+1:*) $
  445.           else if (Index EQ innum -1) THEN InEq = $
  446.                                     InEq(NF:innum-2) $
  447.         else InEq = [InEq(NF:NF+index-1),InEq(NF+index+1:*)]
  448.         if NF NE 0 THEN InEQ = [temp,InEq]
  449.  
  450.         if RP NE 0 THEN writereport,X(InEq,*),Y,Vars(InEq), $
  451.           Vars( VarOut),"Exiting Variable: ","Step", unit
  452.              F = f_to_exit(InEq(NF:*),index,COR,S1,S2)
  453.      ENDWHILE
  454. ENDWHILE
  455.  
  456.      
  457. DONE:
  458.  
  459. writereport,X(InEq,*),Y,Vars(InEq),"*******","*****",  $
  460.                                      "Final",unit
  461. printf,unit," "
  462. printf,unit," "
  463. printf,unit,"Variables in Equation: ",Vars(InEq)
  464. DONE0: ;if N_Elements(M) THEN BEGIN                    ;replace cases with missing data
  465. ;   listrep,x,rownum,rows,here
  466. ;   Y = Fltarr(1,N_Elements(rownum) + sizea)
  467. ;   Y(here) = Y
  468. ;   Y(rownum) = yval
  469. ; ENDIF 
  470. if (unit NE -1) THEN Free_Lun,unit
  471. Return
  472. END
  473.  
  474.  
  475.  
  476.  
  477.  
  478.